/**
 * The base utilities for plugin development
 */
define(function(require, exports) {
  var $ = require('commplugin/jquery');
  //配置信息
  var config={};
  config.dataUrl="http://192.168.5.30:8080/jban/";
  exports.config=config;
  //公共模板
  var Temp={}

  /**
   * base初始化
   */
  exports.init = function(obj) {
  	obj.$(window).bind('hashchange', function(e) {
		var hash = _parseParam(location.hash);
		if (hash.action) {
			obj.actionControl[hash.action](hash);
		}
	});
  }
  

  exports.util = {
    parseParam : _parseParam,
    ajaxFormOpt:_ajaxFormOpt,
    dialog:_dialog
  }
  
  /**
   * 对话框
   */
  function _dialog(opt){
  	var tpl = $.template(Temp.model,opt);
  	$("body").append(tpl);
  	var model=$('#myModal');
  	
  	model.modal({});
  	
  	model.on('hidden', function (e) {
  		if(typeof opt.hidden==="function"){
  			opt.hidden(e);
  		}
  		//移除弹出框
  		model.remove();
	})
  	model.on('hide', function (e) {
  	// do something…
  		if(typeof opt.hide==="function"){
  			opt.hide(e);
  		}
	})
  	model.on('show', function (e) {
  	// do something…
  		if(typeof opt.show==="function"){
  			opt.show(e);
  		}
	})
  	model.on('shown', function (e) {
  	// do something…
  		if(typeof opt.shown==="function"){
  			opt.shown(e);
  		}
	})
  }
  
  /**
   * 得到表单提交信息
   */
  function _ajaxFormOpt(opt){
  	var opts = $.extend({ 
		        target:        '#output1',   // target element(s) to be updated with server response 
		        beforeSubmit:  null,  // pre-submit callback 
		        success:       null,  // post-submit callback 
		        dataType:"jsonp",
        		jsonp:"callback"
		    },opt);
    opts.url = config.dataUrl+opt.url;
  	return opts; 
  }

  
  /*--自定义方法  -----*/
  /**
	 * @description 将?key1=value1&key2=value2&...转换成一个对象{key1:value1,key2:value2....}
	 * @param {String}
	 *            string String字符串
	 * @return {Object} obj 返回结果 {key1:value1,key2:value2....}
	 */
  function _parseParam(str){
	var obj = {};
	if(str==undefined || str==null){
		return obj;	
	}
	
	if(typeof str =='object'){
		return str;
	}
	
	if(typeof str =='string'){
		str = decodeURIComponent(str);
	}
			
	// 踢出前缀#或者？
	str = str.replace(/^[\?\#]/,"");
	// 分割参数
	var params = str.split("&");
	
	for(var i=0;i<params.length;i++){
		var item = params[i].split("=");
		var key = item[0];
        var val = item[1];
		
		if(!key){
			continue;	
		}
		
		// 类型转换
		if(val == undefined){
			val = true;
		}else{
			val = _parseAny(val);
		}
		obj[key] = val;
	}
	return obj;
}


/**
 * @description 把字符串自动转换成它原来的类型
 * @param val
 *            o 任意对象
 * @return val any
 */
	function _parseAny(val){			
		if(typeof val =='string'){
			if(val!="" && !isNaN(val)){
				val = val-0;
			}
			else if(val.toLowerCase()=="true"){
				val = true;
			}
			else if(val.toLowerCase()=="false"){
				val = false;
			}
		}
		return val;
	}
  /*--自定义方法  end -----*/
	
	
	
	/*公共模板*/
	Temp.model='\
<div class="modal fade" id="myModal">\
  <div class="modal-header">\
    <a class="close" data-dismiss="modal">×</a>\
    <h3><%=title %></h3>\
  </div>\
  <div class="modal-body">\
    <p><%=body %></p>\
  </div>\
  <div class="modal-footer">\
	<%if(footer){%>\
    <a href="#" class="btn" data-dismiss="modal">Close</a>\
    <a href="#" class="btn btn-primary">Save changes</a>\
	<%}%>\
  </div>\
</div>';

});

